package org.xl.main.color;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ColorPicker extends JFrame {
    private JButton colorButton;
    private JPanel colorPanel;

    public ColorPicker() {
        setTitle("颜色拾色器");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置支持中文字符集的字体
        Font chineseFont = new Font("微软雅黑", Font.PLAIN, 16);

        colorButton = new JButton("选择颜色");
        colorButton.setFont(chineseFont); // 使用中文支持的字体
        colorPanel = new JPanel();
        colorPanel.setPreferredSize(new Dimension(200, 100));
        colorPanel.setBorder(BorderFactory.createEtchedBorder());

        colorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color selectedColor = JColorChooser.showDialog(null, "选择颜色", colorPanel.getBackground());
                if (selectedColor != null) {
                    colorPanel.setBackground(selectedColor);
                }
            }
        });

        setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(colorButton);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(buttonPanel, BorderLayout.NORTH);
        mainPanel.add(colorPanel, BorderLayout.CENTER);

        add(mainPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ColorPicker colorPicker = new ColorPicker();
                colorPicker.setVisible(true);
                colorPicker.setLocationRelativeTo(null); // 屏幕中央显示
            }
        });
    }
}
